package cn.edu.zsc.blms.service.impl.array;

import cn.edu.zsc.blms.App;
import cn.edu.zsc.blms.domain.Book;
import cn.edu.zsc.blms.domain.Reader;
import cn.edu.zsc.blms.exception.EntityNotExistException;
import cn.edu.zsc.blms.service.BookService;
import cn.edu.zsc.blms.service.ReaderService;
import cn.edu.zsc.blms.util.DateUtil;

import java.text.ParseException;
import java.util.Date;


public class BookServiceImpl extends BaseServiceImpl implements BookService {



    /**
     * 存储图书信息
     */
    public static Book[] DATA = new Book[100];

    /**
     * ID计算器
     */
    private static int idCounter = 0;

    /**
     * 获取用户输入的有效ID值，返回-1表示用户输入的ID值无效的
     *
     * @return ID值
     */
    public int getInputID() {
        if (!App.input.hasNextInt()) {
            App.input.nextLine();
            System.out.println("ID为整数，请输入整数值");
            return -1;
        }
        int id = App.input.nextInt();
        App.input.nextLine();
        if (id >= idCounter || DATA[id] == null) {
            System.out.println("不存在该ID的图书");
            return -1;
        }
        return id;
    }


    @Override
    public void create() {
        if (idCounter >= DATA.length) {
            System.out.println("图书数量已满，不能再添加");
            return;
        }
        System.out.print("请输入书名：");
        String name = App.input.nextLine();
        System.out.print("请输入书号：");
        String isbn = App.input.nextLine();
        System.out.print("请输入类目：");
        String category = App.input.nextLine();
        System.out.print("请输入作者：");
        String author = App.input.nextLine();
        System.out.print("请输入出版社：");
        String press = App.input.nextLine();
        System.out.print("请输入单价，请输入正确格式，暂不对做数据格式检测：");
        System.out.print("请输入出版日期，格式为yyyy-MM-dd:");
        Date publishDate=null;
        while(publishDate==null){
            String strDate=App.input.nextLine();
            try{
                publishDate=DateUtil.parse(strDate);
            }catch(ParseException e){
                System.out.print("输入的日期格式错误，请重写输入：");
            }
        }
        System.out.print("请输入单价：");
        Double price=null;
        while(price==null){
            String strPrice=App.input.nextLine();
            try{
                price=Double.parseDouble(strPrice);
            }catch(NumberFormatException e){
                System.out.print("输入的单价格式错误，请重写输入：");
            }
        }

        Book book = new Book(idCounter, name, isbn, category, author, press,publishDate, price);
//        double price = App.input.nextDouble();
        App.input.nextLine();
//        Book book = new Book(idCounter, name, isbn, category, author, press, price);
        DATA[idCounter] = book;
        System.out.println("成功增加1本图书！");
        idCounter++;
    }

    /**
     * 废弃图书
     */

   @Override
    public void disable(){
        System.out.print("请输入要废弃的图书ID:");
        Book book = null;
        try {
            book = (Book) getOne();
        } catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
        if(book != null){
            book.disabled = true;
            System.out.println("该图书已被废弃:");
        }
    }

    @Override
    public void update() {
        System.out.print("请输入要编辑的图书ID:");
        Book book = null;
        try {
            book = (Book) getOne();
        } catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
        if (book != null) {

            System.out.print("请输入书名，直接按回车表示不修改:");
            String name = App.input.nextLine();
            book.name = "".equals(name) ? book.name : name;
            System.out.print("请输入书号，直接按回车表示不修改:");
            String isbn = App.input.nextLine();
            book.isbn = "".equals(isbn) ? book.isbn : name;
            System.out.print("请输入类目，直接按回车表示不修改：");
            String category = App.input.nextLine();
            book.category = "".equals(category) ? book.category : category;
            System.out.print("请输入作者，直接按回车表示不修改：");
            String author = App.input.nextLine();
            book.author = "".equals(author) ? book.author : author;
            System.out.print("请输入出版社，直接按回车表示不修改：");
            String press = App.input.nextLine();
            book.press = "".equals(press) ? book.press : press;
            System.out.print("请输入单价，请输入正确格式，新不对做数据格式检测:");
            System.out.print("请输入出版日期，格式为yyyy-MM-dd,直接按回车表示不修改：");
            Date publishDate=null;
            while(publishDate==null){
                String strDate=App.input.nextLine();
                if("".equals(strDate)){
                    publishDate=book.publishDate;
                    break;
                }
                try{
                    publishDate=DateUtil.parse(strDate);
                }catch(ParseException e){
                    System.out.print("输入的日期格式错误，请重写输入：");
                }
            }
            book.publishDate = publishDate;
            System.out.print("请输入单价，直接按回车表示不修改:");
            Double price = null;
            while(price==null){
                String strPrice = App.input.nextLine();
                if("".equals(strPrice)){
                    price = book.price;
                    break;
                }
                try{
                    price = Double.parseDouble(strPrice);
                }catch(NumberFormatException e){
                    System.out.print("输入的单价格式错误，请重写输入：");
                }
            }
            book.price=price;
            book.price = App.input.nextDouble();
            App.input.nextLine();
            System.out.println("该圈书的信息已被修改");
        }
    }


    @Override
    public void query() {
        System.out.print("请输入要查询的图书名称: ");
        String name = App.input.nextLine();
        for (int i = 0; i < idCounter; i++) {
            Book book = (Book) DATA[i];
            if(book.name!=null && book.name.contains(name)){
                System.out.println(book);
            }
        }
    }

    /**
     * 单例
     */
    private static BookService singleton;

    /**
     * 私有构造方法
     */
    private BookServiceImpl() {

    }

    /**
     *获取单例，非线性安全
     *
     *
     * @return BookService单例
     */
    public static BookService getSingleton() {
        if (singleton == null) {
            singleton = (BookService) new BookServiceImpl();
        }
        return singleton;
    }
}




